<?php
namespace app\modules\admin\controllers;

use ReflectionClass;
use Yii;
use yii\helpers\Inflector;
use yii\web\Application;

/**
 * 管理后台
 *
 * Class DefaultController
 * @package app\modules\admin\controllers
 */
class DefaultController extends BaseController
{
    /**
     * 管理后台主页
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * 功能搜索
     * @var $keyword string 通过GET提交的查询关键字
     * @return string
     */
    public function actionSearch()
    {
        $controllers = [];
        $module = Yii::$app->getModule('admin');
        $prefix = $module instanceof Application ? '' : $module->getUniqueId() . '/';
        $controllerPath = $module->getControllerPath();
        if (is_dir($controllerPath)) {
            $files = scandir($controllerPath);
            foreach ($files as $file) {
                if (!empty($file) && substr_compare($file, 'Controller.php', -14, 14) === 0) {
                    // $controllerClass = $module->controllerNamespace . '\\' . substr(basename($file), 0, -4);
                    $command = $prefix . Inflector::camel2id(substr(basename($file), 0, -14));
                    $controllers[$command] = [];
                    $description = '';
                    $result = Yii::$app->createController($command);
                    if ($result !== false) {
                        /* @var $controller \yii\base\Controller */
                        list($controller, $actionID) = $result;
                        $reflection = (new ReflectionClass($controller));
                        $docLines = preg_split('~\R~u', $reflection->getDocComment());
                        if (isset($docLines[1])) {
                            $description = trim($docLines[1], "\t *");
                        }
                        $controllers[$command]['description'] = $description;
                        $_actions = [];
                        $class = new ReflectionClass($controller);
                        foreach ($class->getMethods() as $method) {
                            $name = $method->getName();
                            if ($name !== 'actions' && $method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0) {
                                $action_desc = '';
                                $action_name = Inflector::camel2id(substr($name, 6), '-', true);
                                // $action = $controller->createAction($action_name);
                                $reflection = new \ReflectionMethod($controller, $name);
                                $docLines = preg_split('~\R~u', $reflection->getDocComment());
                                if (isset($docLines[1])) {
                                    $action_desc = trim($docLines[1], "\t *");
                                }
                                $_actions[$action_name] = $controllers[$command]['description'] . ' &gt; ' . $action_desc;
                            }
                        }
                        $controllers[$command]['actions'] = $_actions;
                    }
                }
            }
        }
        $keyword = Yii::$app->request->get('keyword');
        $result = [];
        foreach ($controllers as $command=>$controller) {
            foreach ($controller['actions'] as $action=>$desc) {
                if (empty($keyword) || strpos($desc, $keyword) !== false) {
                    $result['/' . $command . '/' . $action] = $desc;
                }
            }
        }
        return $this->render('search', [
            'result'=>$result,
            'keyword'=>$keyword
        ]);
    }
    
    /**
     * 待办事项
     * @return string
     */
    public function actionTodo()
    {
        return $this->render('todo');
    }
}
